package com.session7;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class HttpResquestDemo {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			URL url = new URL("http://localhost/doAdd.php");
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			conn.setRequestMethod("POST");
			conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
			conn.setRequestProperty("charset", "UTF-8");
			conn.setDoOutput(true);
			conn.setDoInput(true);
			String data = "user=ngocnv&pass=123";
			conn.setRequestProperty("Content-Length", data.length()+"");
			conn.connect();
			
			// Gui data di
			OutputStream out = conn.getOutputStream();
			out.write(data.getBytes());
			out.flush();
			out.close();
			
			// Nhan du lieu tren web tra ve
			InputStream in = conn.getInputStream();
			byte[] b = new byte[in.available()];
			in.read(b);
			System.out.println(new String(b));
			in.close();
			// Dong ket noi
			conn.disconnect();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}

}
